package com.yuncheng.spcyApi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.bo.ParamsBo;
import com.yuncheng.spcyApi.spcyService.SpcyGzglService;
import com.yuncheng.spcyApi.dto.SpcyGzjlDto;
import com.yuncheng.spcyApi.entity.SpcyOaGzjl;
import com.yuncheng.spcyApi.entity.SpcyOaGztclx;
import com.yuncheng.spcyApi.entity.SpcyOaGzxk;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.spcy.SpcyOaGzjlVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;


/**
 * @Description: 审评查验-工资管理
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Api(tags = "审评查验-工资管理")
@RestController
@RequestMapping("/api/spzx-spcy/SpcyGzgl")
public class SpcyGzglController {

    private static final Logger log = LoggerFactory.getLogger(SpcyGzglController.class);

    @Resource
    private SpcyGzglService spcyGzglService;



    @Log(title = "分页查询-工资项库")
    @ApiOperation(value = "分页查询-工资项库")
    @GetMapping(value = "queryPageGzxkList")
    public HttpResult queryPageGzxkList(SpcyOaGzxk gzxk,
                                        @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        return spcyGzglService.queryPageGzxkList(gzxk,pageNo,pageSize);
    }

    @Log(title = "分页查询-工资套餐类型")
    @ApiOperation(value = "分页查询-工资套餐类型")
    @GetMapping(value = "queryPageGztclxList")
    public HttpResult queryPageGztclxList(SpcyOaGztclx gztclx,
                                          @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                          @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        return spcyGzglService.queryPageGztclxList(gztclx,pageNo,pageSize);
    }

    @Log(title = "分页查询-工资记录")
    @ApiOperation(value = "分页查询-工资记录")
    @GetMapping(value = "queryPageGzjlList")
    public HttpResult queryPageGzjlList(SpcyOaGzjl gzjl,
                                        @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        return spcyGzglService.queryPageGzjlList(gzjl,pageNo,pageSize,false);
    }

    @Log(title = "分页查询-个人工资记录")
    @ApiOperation(value = "分页查询-个人工资记录")
    @GetMapping(value = "queryPageMyGzjlList")
    public HttpResult queryPageMyGzjlList(SpcyOaGzjl gzjl,
                                        @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        return spcyGzglService.queryPageGzjlList(gzjl,pageNo,pageSize,true);
    }

    @Log(title = "分页查询-工资记录详情统计")
    @ApiOperation(value = "分页查询-工资记录详情统计")
    @GetMapping(value = "queryGzjlxqPage")
    public HttpResult queryGzjlxqPage(SpcyOaGzjl gzjl,
                                 @RequestParam(value = "gzxs",defaultValue = "") String gzxs,
                                 @RequestParam(value = "startYear",defaultValue = "") String startYear,
                                 @RequestParam(value = "endYear",defaultValue = "") String endYear,
                                 @RequestParam(value = "startMonth",defaultValue = "") String startMonth,
                                 @RequestParam(value = "endMonth",defaultValue = "") String endMonth,
                                 @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        Page page = new Page<SpcyGzjlDto>(pageNo,pageSize);
        return spcyGzglService.findGzjlxqPage(page, gzjl, gzxs, startYear, endYear, startMonth, endMonth);
    }

    @Log(title = "获取工资记录详情统计-工资项合计")
    @ApiOperation(value = "分页查询-获取工资记录详情统计-工资项合计")
    @GetMapping(value = "getGzjlxqHj")
    public HttpResult getGzjlxqHj(SpcyOaGzjl gzjl,
                                      @RequestParam(value = "gzxs",defaultValue = "") String gzxs,
                                      @RequestParam(value = "startYear",defaultValue = "") String startYear,
                                      @RequestParam(value = "endYear",defaultValue = "") String endYear,
                                      @RequestParam(value = "startMonth",defaultValue = "") String startMonth,
                                      @RequestParam(value = "endMonth",defaultValue = "") String endMonth){

        String hj = spcyGzglService.getGzjlxqHj(gzjl, gzxs, startYear, endYear, startMonth, endMonth);
        HttpResult result = new HttpResult(true);
        result.setMessage("操作成功");
        result.setResult(hj);
        return result;
    }

    @Log(title = "获取所有工资项库数据")
    @ApiOperation(value = "获取所有工资项库数据")
    @ApiImplicitParam(name = "fmc",value = "工资项名称")
    @GetMapping(value = "getAllGzxkList")
    public HttpResult getAllGzxkList(@RequestParam(value = "fmc",defaultValue = "")String fmc){

        return spcyGzglService.getAllGzxkList(fmc);
    }

    @Log(title = "获取所有工资套餐类型数据")
    @ApiOperation(value = "获取所有工资套餐类型数据")
    @GetMapping(value = "getAllGztclxList")
    public HttpResult getAllGztclxList(){

        return spcyGzglService.getAllGztclxList();
    }

    @Log(title = "获取所有工资的姓名数据")
    @ApiOperation(value = "获取所有工资的姓名数据")
    @GetMapping(value = "getAllGzjlUserList")
    public HttpResult getAllGzjlUserList(){

        return spcyGzglService.getAllGzjlUserList();
    }

    @Log(title = "工资项库-添加")
    @ApiOperation(value = "工资项库-添加")
    @PostMapping(value = "addGzxk")
    public HttpResult addGzxk(@RequestBody SpcyOaGzxk gzxk) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (StringUtils.isNotBlank(gzxk.getId())){
            return spcyGzglService.updateGzxk((HashMap<String, String>) BeanUtils.describe(gzxk));
        }
        return spcyGzglService.addGzxk(gzxk);
    }

    @Log(title = "工资套餐类型-添加")
    @ApiOperation(value = "工资套餐类型-添加")
    @PostMapping(value = "addGztclx")
    public HttpResult addGztclx(@RequestBody SpcyOaGztclx gztclx) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (StringUtils.isNotBlank(gztclx.getId())){
            return spcyGzglService.updateGztclx((HashMap<String, String>) BeanUtils.describe(gztclx));
        }
        return spcyGzglService.addGztclx(gztclx);
    }

    @Log(title = "工资套餐-添加")
    @ApiOperation(value = "工资套餐-添加")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "gztclxid",value = "工资套餐类型id"),
            @ApiImplicitParam(name = "gzxkids",value = "工资项库id(可多个id，逗号拼接)")
    })
    @PostMapping(value = "addGztc")
    public HttpResult addGztc(@RequestBody Map<String,String> paramMap) {
        return spcyGzglService.addGztc(paramMap);
    }

    @Log(title = "工资项库-更新")
    @ApiOperation(value = "工资项库-更新")
    @PutMapping(value = "updateGzxk")
    public HttpResult updateGzxk(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        return spcyGzglService.updateGzxk(buffer);
    }

    @Log(title = "工资套餐类型-更新")
    @ApiOperation(value = "工资套餐类型-更新")
    @PutMapping(value = "updateGztclx")
    public HttpResult updateGztclx(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        return spcyGzglService.updateGztclx(buffer);
    }

    @Log(title = "工资套餐-更新")
    @ApiOperation(value = "工资套餐-更新")
    @PutMapping(value = "updateGztc")
    public HttpResult updateGztc(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        return spcyGzglService.updateGztc(buffer);
    }

    @Log(title = "删除工资项库-通过工资项库id")
    @ApiOperation(value = "删除工资项库-通过工资项库id")
    @DeleteMapping(value = "deleteGzxk")
    public HttpResult deleteGzxk(String id){
        return spcyGzglService.deleteGzxk(id);
    }

    @Log(title = "删除工资套餐类型-通过工资套餐类型id")
    @ApiOperation(value = "删除工资套餐类型-通过工资套餐类型id")
    @DeleteMapping(value = "deleteGztclx")
    public HttpResult deleteGztclx(String id){
        return spcyGzglService.deleteGztclx(id);
    }

    @Log(title = "删除工资套餐-通过工资套餐id")
    @ApiOperation(value = "删除工资套餐-通过工资套餐id")
    @DeleteMapping(value = "deleteGztc")
    public HttpResult deleteGztc(String id){
        return spcyGzglService.deleteGztc(id);
    }

    @Log(title = "删除工资记录-通过工资记录id")
    @ApiOperation(value = "删除工资记录-通过工资记录id")
    @DeleteMapping(value = "deleteGzjl")
    public HttpResult deleteGzjl(String id){
        return spcyGzglService.deleteGzjl(id);
    }

    @Log(title = "查询工资项库-通过工资项库id查询")
    @ApiOperation(value = "查询工资项库-通过工资项库id查询")
    @GetMapping(value = "queryGzxkById")
    public HttpResult queryGzxkById(String id){
        return spcyGzglService.queryGzxkById(id);
    }

    @Log(title = "查询工资套餐类型详情-通过工资套餐类型id查询")
    @ApiOperation(value = "查询工资套餐类型详情-通过工资套餐类型id查询")
    @GetMapping(value = "queryGztclxById")
    public HttpResult queryGztclxById(String id){
        return spcyGzglService.queryGztclxById(id);
    }

    @Log(title = "查询工资套餐-通过工资套餐id")
    @ApiOperation(value = "查询工资套餐-通过工资套餐id")
    @GetMapping(value = "queryGztcById")
    public HttpResult queryGztcById(String id){
        return spcyGzglService.queryGztcById(id);
    }

    @Log(title = "工资记录详情-通过工资记录id查询")
    @ApiOperation(value = "工资记录详情-通过工资记录id查询")
    @GetMapping(value = "queryGzjlxqById")
    public HttpResult queryGzjlxqById(String id){
        return spcyGzglService.queryGzjlxqById(id);
    }

    @Log(title = "发送工资记录-确认状态-已确认")
    @ApiOperation(value = "发送工资记录-确认状态-已确认")
    @PostMapping(value = "sendGzjlQrzt")
    public HttpResult sendGzjlQrzt(@RequestBody ParamsBo paramsBo){
        spcyGzglService.sendGzjlQrzt(paramsBo.getIds());

        return HttpResult.ok("确认成功");
    }

    @Log(title = "导入工资套餐类型模板数据-表格Excel")
    @ApiOperation(value = "导入工资套餐类型模板数据-表格Excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "file",value = "excel模板文件",required = true),
            @ApiImplicitParam(name = "type",value = "工资套餐类型",required = true)
    })
    @PostMapping(value = "importGztclxFile")
    public HttpResult<?> importGztclxFile(@RequestParam("file") MultipartFile file,
                                          @RequestParam(value = "type",defaultValue = "")String type){

        //获取文件名
        String fileName = file.getOriginalFilename();
        //判断文件类型，这里我们只要xls文件
        if (fileName != null && !fileName.endsWith(".xls") && !fileName.endsWith(".xlsx")) {
            return HttpResult.error(1, "请选择“xls”或“xlsx”文件");
        }
        spcyGzglService.importGztclxFile(file,fileName,type);
        return HttpResult.ok("导入成功");
    }

    @Log(title = "导出工资套餐类型对应模板-通过工资套餐类型id")
    @ApiOperation(value = "导出工资套餐类型对应模板-通过工资套餐类型id")
    @GetMapping(value = "exportGztclxMb")
    public void exportGztclxMb(String id, HttpServletResponse response){
        spcyGzglService.exportGztclxMb(id,response);
    }

    @Log(title = "导出工资记录数据详情-Excel文件和Png文件的压缩包文件")
    @ApiOperation(value = "导出工资记录数据详情-Excel文件和Png文件的压缩包文件")
    @GetMapping(value = "exportGztclxDataZipFile")
    public void exportGztclxDataZipFile(SpcyOaGzjlVo gzjl,HttpServletResponse response){
        spcyGzglService.exportGztclxDataFile(gzjl,response);
    }

    @Log(title = "下载Png图片文件-工资记录详情")
    @ApiOperation(value = "下载Png图片文件-工资记录详情")
    @ApiImplicitParam(name = "id",value = "工资记录id")
    @GetMapping(value = "downGzjlxqPngFile")
    public void downGzjlxqPngFile(@RequestParam(value = "id",defaultValue = "") String id,HttpServletResponse response){
        spcyGzglService.downGzjlxqPngFile(id,response);
    }

    @Log(title = "下载excel文件-工资记录详情")
    @ApiOperation(value = "下载excel文件-工资记录详情")
    @ApiImplicitParam(name = "id",value = "工资记录id")
    @GetMapping(value = "downGzjlxqExcelFile")
    public void downGzjlxqExcelFile(@RequestParam(value = "id",defaultValue = "") String id,HttpServletResponse response){
        spcyGzglService.downGzjlxqExcelFile(id,response);
    }

    /*@Log(title = "下载工资记录统计Excel文件")
    @ApiOperation(value = "下载工资记录统计Excel文件")
    @GetMapping(value = "downloadGzjlStat")*/
    public void downloadGzjlStat(SpcyOaGzjl gzjl,
                                 @RequestParam(value = "gzxs",defaultValue = "") String gzxs,
                                 @RequestParam(value = "startYear",defaultValue = "") String startYear,
                                 @RequestParam(value = "endYear",defaultValue = "") String endYear,
                                 @RequestParam(value = "startMonth",defaultValue = "") String startMonth,
                                 @RequestParam(value = "endMonth",defaultValue = "") String endMonth,
                                 HttpServletResponse response){
        spcyGzglService.downloadGzjlStat(gzjl, gzxs, startYear, endYear, startMonth, endMonth, response);
    }

}
